/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

/**
 *
 * @author sbavdhorst
 */
public class Quartet {
    private char[] pair1, pair2;
    
    public Quartet(char[] pair1, char[] pair2){
        this.pair1 = pair1;
        this.pair2 = pair2;
    }
    
    public char[] getPair1(){
        return pair1;
    }
    
    public char[] getPair2(){
        return pair2;
    }
    
    private char[] getTaxa(){
        return new char[]{pair1[0], pair1[1], pair2[0], pair2[1]};
    }
    
    public boolean sameTaxa(Quartet q){
        for(char c: q.getTaxa()){
            if(!this.contains(c))
                return false;
        }
        return true;
    }
    
    public boolean equals(Quartet q){
        return (q.getPair1()[0] == this.pair1[0] || q.getPair1()[0] == this.pair1[1]) &&
               (q.getPair1()[1] == this.pair1[0] || q.getPair1()[1] == this.pair1[1]) &&
               (q.getPair2()[0] == this.pair2[0] || q.getPair2()[0] == this.pair2[1]) &&
               (q.getPair2()[1] == this.pair2[0] || q.getPair2()[1] == this.pair2[1]);
    }
    
    public boolean contains(char c){
        return pair1[0] == c || pair1[1] == c || pair2[0] == c || pair2[1] == c;
    }
    
    public boolean containsPair(char c1, char c2){
        return (pair1[0] == c1 && pair1[1] == c2) || (pair1[1] == c1 && pair1[0] == c2) ||
                (pair2[0] == c1 && pair2[1] == c2) || (pair2[1] == c1 && pair2[0] == c2);
    }
    
     public boolean pairContains(boolean p1, char c){
        if(p1)
            return pair1[0] == c || pair1[1] == c;
        else
            return pair2[0] == c || pair2[1] == c;
    }
    
//    public int circleSimilarity(Circle c){
//        int score = 0;
//        if(c.contains(pair1[0]))
//            score++;
//        if(c.contains(pair1[1]))
//            score++;
//        if(c.contains(pair2[0]))
//            score++;
//        if(c.contains(pair2[1]))
//            score++;
//        return score;
//    }
     
    public int similarity(Quartet q){
        int score = 0;
        if(q.contains(pair1[0]))
            score++;
        if(q.contains(pair1[1]))
            score++;
        if(q.contains(pair2[0]))
            score++;
        if(q.contains(pair2[1]))
            score++;
        return score;
    }
    
    public boolean isOnCircle(Circle circle){
        boolean firstLeft1 = false, firstLeft2 = false, firstRight = false, secondRight = false;
        for(char c: circle.getCircle()){
            if(pair1[0] == c || pair1[1] == c){
                if(!firstLeft1 && !firstLeft2 && !firstRight)
                    firstLeft1 = true;
                else if(!firstLeft1 && !firstLeft2)
                    firstLeft2 = true;
                else{
                    if((firstLeft1 || firstLeft2) && (firstRight && secondRight) || (!firstRight && !secondRight) || (firstRight && firstLeft2))
                        return true;
                    else
                        return false;
                }
            } else if(pair2[0] == c || pair2[1] == c){
                if(!firstRight)
                    firstRight = true;
                else if(firstLeft2)
                    return false;
                else
                    return true;
            }
        }
        return false;
    }
    
    @Override
    public String toString(){
        StringBuilder s = new StringBuilder();
        s.append(pair1[0]);
        s.append(pair1[1]);
        s.append('|');
        s.append(pair2[0]);
        s.append(pair2[1]);
        return s.toString();
    }
    
    public static void main(String[] args){
        Circle circle= new Circle(7);
        circle.add(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'});
        Quartet q = new Quartet(new char[]{'a','b'}, new char[]{'c', 'd'});
        System.out.println(q.isOnCircle(circle));
    }
}
